
#include "LitePipelineInfo.h"

using namespace ttg_internal::pipeline;

//--------------------
//--- LiteNodeInfo ---
//--------------------

LiteNodeInfo::LiteNodeInfo(NodeInfo *node)
{
	if (node != NULL)
	{
		this->name = node->getFilter()->getID();
		this->active_task_count = node->getActiveTaskCount().getID();
		this->usage = node->getCpuUsage().getID();
		for (size_t i = 0; i < node->getNext().size(); i++)
			this->outputs.push_back(node->getNext()[i]->getFilter()->getID());
	}
	else
	{
		this->name = 0;
		this->active_task_count = 0;
		this->usage = 0;
	}
}

void LiteNodeInfo::serialize(ttg::extras::serialization::Serializer &serializer)
{
	serializer.serializeInt((int)name, "name");
	serializer.serializeInt((int)active_task_count, "active_task_count");
	serializer.serializeInt((int)usage, "usage");
	serializer.serializeInt((int)outputs.size(), "outputs.size()");
	for (size_t i = 0; i < outputs.size(); i++)
		serializer.serializeInt((int)outputs[i], "outputs[...]");
}

void LiteNodeInfo::deserialize(ttg::extras::serialization::DeSerializer &deserializer)
{
	int name;
	int active_task_count;
	int usage;
	int outputs_size;
	int output;
	deserializer.deserializeInt(name, "name");
	deserializer.deserializeInt(active_task_count, "active_task_count");
	deserializer.deserializeInt(usage, "usage");
	deserializer.deserializeInt(outputs_size, "outputs.size()");
	this->name = name;
	this->active_task_count = active_task_count;
	this->usage = usage;
	outputs.resize(outputs_size);
	for (size_t i = 0; i < outputs.size(); i++)
	{
		deserializer.deserializeInt(output, "outputs[...]");
		outputs[i] = output;
	}
}

void LiteNodeInfo::serializeNodes(std::vector<LiteNodeInfo> &nodes,
								  ttg::extras::serialization::Serializer &serializer)
{
	serializer.serializeInt((int)nodes.size(), "nodes.size()");
	for (size_t i = 0; i < nodes.size(); i++)
	{ serializer.serializeObject(&nodes[i], "nodes[...]"); }
}

std::vector<LiteNodeInfo> LiteNodeInfo::deserializeNodes(
					ttg::extras::serialization::DeSerializer &deserializer)
{
	int size;
	deserializer.deserializeInt(size, "node.size()");
	std::vector<LiteNodeInfo> nodes;
	nodes.resize((size_t)size);
	for (size_t i = 0; i < nodes.size(); i++)
	{
		LiteNodeInfo *n = &nodes[i];
		deserializer.deserializeObject(n, "nodes[...]");
	}
	return nodes;
}

//---------------------------
//--- LiteSubPipelineInfo ---
//---------------------------

void LiteSubPipelineInfo::serialize(ttg::extras::serialization::Serializer &serializer)
{
	serializer.serializeInt((int)name, "name");
	LiteNodeInfo::serializeNodes(nodes, serializer);
}

void LiteSubPipelineInfo::deserialize(ttg::extras::serialization::DeSerializer &deserializer)
{
	int name;
	deserializer.deserializeInt(name, "name");
	this->name = name;
	this->nodes = LiteNodeInfo::deserializeNodes(deserializer);
}

//------------------------
//--- LitePipelineInfo ---
//------------------------

LitePipelineInfo::LitePipelineInfo(PipelineInfo *ref)
{
	if (ref != NULL)
	{
		name = ref->getFilter()->getID();
		for (size_t i = 0; i < ref->getNodes().size(); i++)
			nodes.push_back(LiteNodeInfo(ref->getNodes()[i]));
	}
	else
	{ name = 0; }
}

void LitePipelineInfo::serialize(ttg::extras::serialization::Serializer &serializer)
{
	serializer.serializeInt((int)name, "name");
	LiteNodeInfo::serializeNodes(nodes, serializer);
	serializer.serializeInt((int)sub_pipelines.size(), "sub_pipelines.size()");
	for (size_t i = 0; i < sub_pipelines.size(); i++)
		serializer.serializeObject(&sub_pipelines[i], "sub_pipeline[...]");
}

void LitePipelineInfo::deserialize(ttg::extras::serialization::DeSerializer &deserializer)
{
	int name;
	int sub_pipelines_size;
	//Name.
	deserializer.deserializeInt(name, "name");
	this->name = name;
	//Nodes.
	nodes = LiteNodeInfo::deserializeNodes(deserializer);
	//Sub-pipelines.
	deserializer.deserializeInt(sub_pipelines_size, "sub_pipelines.size()");
	sub_pipelines.resize(sub_pipelines_size);
	for (size_t i = 0; i < sub_pipelines.size(); i++)
	{
		LiteSubPipelineInfo *sp = &sub_pipelines[i];
		deserializer.deserializeObject(sp, "sub_pipeline[...]");
	}
}
